home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Kernel / Em / avg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-17  |  2.4 KB  |  106 lines

  1. /*
  2.  * Utility Routines
  3.  *
  4.  * Return the machine load average as a long int.  This needs a lot of work
  5.  * since it is very Unix
  6.  * oriented.  This is a rip-off of /usr/src/cmd/w.c with the
  7.  * provision that we do not do an nlist or open a file on every
  8.  * call, just the first one.  It returns the one-minute load
  9.  * average, multiplied by 100 and then TRUNCATED.
  10.  *
  11.  * WARNING: avenrun is an array of floating points on the VAXen
  12.  * but an array of fixed point (ints) on the SUNs, which have
  13.  * no hardware floating point.
  14.  *
  15.  * This is a rip-off from Eden.
  16.  */
  17.  
  18. #include <nlist.h>
  19. #include "Kernel/h/system.h"
  20. #include "Kernel/h/kmdTypes.h"
  21. #ifdef sun
  22. #include <sys/param.h>
  23. #endif
  24.  
  25. static struct nlist nl[] = {
  26.     { "_avenrun" },
  27. #define X_AVENRUN 0
  28.     {      0     }
  29. };
  30.  
  31. static int kmem = -1;
  32. static int opened = 0;
  33.  
  34. #if defined(vax)
  35. static double avenrun[3];
  36. #endif
  37. #if defined(sun)
  38. static long avenrun[3];
  39. #endif
  40.  
  41. #ifndef NODISK
  42. extern char nodisk;
  43. #endif
  44.  
  45. int MachineLoad()
  46. {
  47. #ifndef NODISK
  48.   if(nodisk){
  49. #endif
  50.     return 1;
  51. #ifndef NODISK
  52.   } else {
  53.     if ( ! opened ) {
  54.     opened = 1;
  55.     kmem = open("/dev/kmem",0);
  56.     if ( kmem < 0 ) return( 1000000 );
  57.     nlist("/vmunix", nl);
  58.     if ( nl[0].n_type == 0 ) return( 10000000 );
  59.     }
  60.     if ( kmem < 0 ) return( 1000000 );
  61.     if ( nl[0].n_type == 0 ) return( 10000000 );
  62.     (void) lseek(kmem, (long)nl[X_AVENRUN].n_value, 0 );
  63.     (void) read(kmem, (char *) avenrun, sizeof(avenrun));
  64. #if defined(vax)
  65.     return( (int) (avenrun[1] * 100.0) );
  66. #endif
  67. #if defined(sun)
  68.     /* FSCALE is defined in /usr/include/sys/param.h on SUNs */
  69.     return( avenrun[1]*100/FSCALE );
  70. #endif
  71.   }
  72. #endif
  73. }
  74.  
  75. float MachineLoadAvg()
  76. /* same but return a float */
  77. {
  78. #ifndef NODISK
  79.   if(nodisk){
  80. #endif
  81.     return 1.0;
  82. #ifndef NODISK
  83.   } else {
  84.     if ( ! opened ) {
  85.     opened = 1;
  86.     KMDTrace("FixMe", 6, "Might have to fix this for 4.3\n");
  87.     kmem = open("/dev/kmem",0);
  88.     if ( kmem < 0 ) return( 1000000 );
  89.     nlist("/vmunix", nl);
  90.     if ( nl[0].n_type == 0 ) return( 10000000 );
  91.     }
  92.     if ( kmem < 0 ) return( 10000000 );
  93.     if ( nl[0].n_type == 0 ) return( 10000000 );
  94.     (void) lseek(kmem, (long)nl[X_AVENRUN].n_value, 0 );
  95.     (void) read(kmem, (char *) avenrun, sizeof(avenrun));
  96. #if defined(vax)
  97.     return( (float) (avenrun[1]) );
  98. #endif
  99. #if defined(sun)
  100.     /* FSCALE is defined in /usr/include/sys/param.h on SUNs */
  101.     return( (double) avenrun[1] / (double) FSCALE );
  102. #endif
  103.   }
  104. #endif
  105. }
  106.